package com.byyy.ccts.modules.sys.service.impl;

import com.byyy.ccts.core.common.service.impl.CommonServiceImpl;
import com.byyy.ccts.core.query.wrapper.EntityWrapper;
import com.byyy.ccts.core.utils.CacheUtils;
import com.byyy.ccts.core.utils.StringUtils;
import com.byyy.ccts.modules.sys.entity.*;
import com.byyy.ccts.modules.sys.mapper.UserMapper;
import com.byyy.ccts.modules.sys.service.IUserOrganizationService;
import com.byyy.ccts.modules.sys.service.IUserRoleService;
import com.byyy.ccts.modules.sys.service.IUserService;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

@Transactional
@Service("userService")
public class UserServiceImpl extends CommonServiceImpl<UserMapper, User> implements IUserService {
	@Autowired
	PasswordService passwordService;
	@Autowired
	private IUserOrganizationService userOrganizationService;
	@Autowired
	private IUserRoleService userRoleService;

	@Override
	public void changePassword(String userid, String newPassword) {
		User user = selectById(userid);
		if (user != null) {
			user.setPassword(newPassword);
			passwordService.encryptPassword(user);
		}
		insertOrUpdate(user);
	}

	@Override
	public User findByUsername(String username) {
		if (StringUtils.isEmpty(username)) {
			return null;
		}
		return selectOne(new EntityWrapper<User>(User.class).eq("username", username));
	}

	@Override
	public User findByEmail(String email) {
		if (StringUtils.isEmpty(email)) {
			return null;
		}
		return selectOne(new EntityWrapper<User>(User.class).eq("email", email));
	}

	@Override
	public User findByPhone(String phone) {
		if (StringUtils.isEmpty(phone)) {
			return null;
		}
		return selectOne(new EntityWrapper<User>(User.class).eq("phone", phone));
	}

	@Override
	public List<Yez> queryYezByUsername(String username) {
		List<Yez> yezList = (List) CacheUtils.get(CacheUtils.USER_CACHE, username + "_YEZ");
		if (yezList == null || yezList.size() < 1) {
			yezList = this.baseMapper.queryYezByUsername(username);
			CacheUtils.put(CacheUtils.USER_CACHE, username + "_YEZ", yezList);
		}
		return yezList;
	}

	@Override
	public List<Cangku> queryYezCangkuByUsername(String username, String yezId) {
		List<Cangku> cangkuList = (List) CacheUtils.get(CacheUtils.USER_CACHE, username + "_" + yezId + "_CANGKU");
		if (cangkuList == null || cangkuList.size() < 1) {
			cangkuList = this.baseMapper.queryYezCangkuByUsername(username, yezId);
			CacheUtils.put(CacheUtils.USER_CACHE, username + "_" + yezId + "_CANGKU", cangkuList);
		}
		return cangkuList;
	}

	@Override
	public List<Cangku> queryCangkuByUsername(String username) {
		List<Cangku> cangkuList = (List) CacheUtils.get(CacheUtils.USER_CACHE, username + "_" + "_CANGKU");
		if (cangkuList == null || cangkuList.size() < 1) {
			cangkuList = this.baseMapper.queryCangkuByUsername(username);
			CacheUtils.put(CacheUtils.USER_CACHE, username + "_" + "_CANGKU", cangkuList);
		}
		return cangkuList;
	}

	@Override
	public boolean insert(User user) {
		passwordService.encryptPassword(user);
		return super.insert(user);
	}

	@Override
	public boolean deleteById(Serializable id) {
		// 删除角色关联
		userRoleService.delete(new EntityWrapper<UserRole>(UserRole.class).eq("userId", id));
		// 删除部门关联
		userOrganizationService.delete(new EntityWrapper<UserOrganization>(UserOrganization.class).eq("userId", id));
		return super.deleteById(id);
	}

	@Override
	public boolean deleteBatchIds(List<? extends Serializable> idList) {
		for (Object id : idList) {
			this.deleteById((Serializable) id);
		}
		return true;
	}

	@Override
	public Page<User> selectPage(Page<User> page, Wrapper<User> wrapper) {
		wrapper.eq("1", "1");
		page.setRecords(baseMapper.selectUserList(page, wrapper));
		return page;
	}

}
